
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { UserService } from '@global/shared/user.service';
import {Environment} from '../../environments/environment';
@Injectable({
  providedIn: 'root'
})
export class FcsServiceStartupService {
  constructor(private userService: UserService, private http: HttpClient) {
    FcsServiceStartupService.regionList = [];
    FcsServiceStartupService.regionStorageClassList = []; //region->storageclass
    FcsServiceStartupService.storageClassList = [];
    FcsServiceStartupService.storageClassRegionList = [];// storageclass->region
  }

  static regionList: any[];
  static regionStorageClassList: any[];
  static storageClassList: any[];
  static storageClassRegionList: any[];
  static STORAGECLASS = { STANDARD: 'STANDARD', STANDARD_IA: 'STANDARD_IA' };
  STORAGECLASS_BSS = { STANDARD: 'OSS', STANDARD_IA: 'OSS_IA' };

  async load() {
    // get regionStorageClass
    let userId = this.userService.getUserId();
    let bssDetail = await this.getProductList(
      userId
    ).toPromise();
    let resProductTypeList;
    if (bssDetail) {
      resProductTypeList =
        bssDetail.result.productLineList[0].productTypeList || [];

    } else {
      resProductTypeList = [];
    }
    this.format(resProductTypeList);
    this.getRegionStorageClassList(FcsServiceStartupService.storageClassRegionList);
  }
  /**
   * InbojServiceStartupService.regionList
   * InbojServiceStartupService.regionStorageClassList
   * @param resProductTypeList 接口返回的产品列表数据格式
   */
  format(resProductTypeList) {
    let object = [];
    let tempRegionList = [];
    let storageClassList = [];
    for (const key in resProductTypeList) {
      if (resProductTypeList.hasOwnProperty(key)) {
        const element = resProductTypeList[key];
        switch (element.code) {
          case this.STORAGECLASS_BSS.STANDARD:
            element.code = FcsServiceStartupService.STORAGECLASS.STANDARD;
            break;
          case this.STORAGECLASS_BSS.STANDARD_IA:
            element.code = FcsServiceStartupService.STORAGECLASS.STANDARD_IA;
            break;
          default:
            break;
        }
        const { code, name, regionList } = element;
        object = [...object, { code: code, name: name, regionList: regionList }];
        storageClassList = [...storageClassList, { code: code, name: name }];

        regionList.map((item) => {
          tempRegionList = [...tempRegionList, { regionId: item.code, regionName: item.name }];
        });
      }
    }
    // 去重
    tempRegionList = Array.from(new Set(tempRegionList.map(item => JSON.stringify(item))));
    FcsServiceStartupService.regionList = tempRegionList.map(item => JSON.parse(item));
    FcsServiceStartupService.storageClassRegionList = object;
    FcsServiceStartupService.storageClassList = storageClassList;
  }
  getRegionStorageClassList(storageClassRegionList) {
    let regionList = [].concat(JSON.parse(JSON.stringify(FcsServiceStartupService.regionList)));
    let object = storageClassRegionList;
    for (const key in object) {
      if (object.hasOwnProperty(key)) {
        const element = object[key];
        // 类型下对应的regionlist
        element.regionList.forEach(item => {
          // regionlist
          regionList.forEach(region => {
            if (item.code === region.regionId) {
              region.storageClass = region.storageClass || [];
              region.storageClass.push({ code: element.code, name: element.name });
            }
          });
        });
      }
    }
    FcsServiceStartupService.regionStorageClassList = regionList;

  }


  /**
   * 函数计算类型.
   */
  getProductList(userId: string): Observable<any> {
    let url = Environment.application.bssAPI + '/product/';
    return this.http.get(url, {
      params: {
        userId: userId,
        productLineCode: 'FCS'
      }
    });
  }
}
